package com.tangcco.lockwell.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.Inform;
import com.tangcco.lockwell.domain.dto.inform.InformDto;
import com.tangcco.lockwell.repository.BaseMapper;
import com.tangcco.lockwell.domain.dto.inform.InformDto;
import com.tangcco.lockwell.repository.InformMapper;
import com.tangcco.lockwell.repository.UserMapper;
import com.tangcco.lockwell.service.InformService;
import com.tangcco.lockwell.viewmodel.admin.Inform.AcceptInformVM;
import com.tangcco.lockwell.viewmodel.admin.Inform.InformVM;
import com.tangcco.lockwell.viewmodel.admin.user.UserGroupVm;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

import static com.tangcco.lockwell.utility.DateToString.dateToString;

@Service
public class InformServiceImpl implements InformService {

    @Resource
    private InformMapper informMapper;

    @Resource
    private UserMapper userMapper;


    @Autowired
    protected WebContext webContext;

    @Override
    public int updateIsRead(Integer id) {
        InformVM inform = new InformVM();
        inform.setInformId(id);
        inform.setModifyTime(dateToString(new Date()));
        informMapper.updateIsRead(inform);
        return 1;
    }
    @Override
    public int insertSelectiveNotNull(Inform inform) {
        return informMapper.insertSelectiveNotNull(inform);
    }


    @Override
    public PageInfo<InformVM> selectSelective(InformVM informVM) {
        PageHelper.startPage(informVM.getPageIndex(),informVM.getPageSize());
        List<InformVM> informVMS = informMapper.selectSelective(informVM);
        PageInfo<InformVM> pageInfo = new PageInfo<>(informVMS);
        return pageInfo;
    }



    @Override
    public int deleteByPrimaryKey(Integer id) {
        informMapper.deleteInform(id);
        return informMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public int indesert(Inform record, int[] uids, Integer isAll) {
        if (isAll == 0){
            //查询所有用户ID，封装数组
            List<Integer> integers = userMapper.selectAllUserIds();
            uids = integers.stream()
                    .mapToInt(Integer::intValue)
                    .distinct()
                    .toArray();
        }
        else if (isAll==3){
            //查询所有用户ID，管理员除外，封装数组
            List<Integer> integers = userMapper.selectUserIds(1);
            uids = integers.stream()
                    .mapToInt(Integer::intValue)
                    .distinct()
                    .toArray();
        }else if (isAll==2){
            uids=Arrays.stream(uids)
                    .distinct()
                    .toArray();
        }else if (isAll == 4){
            //查询所有用户ID，管理员除外，封装数组
            List<Integer> integers = userMapper.selectUserIds(3);
            uids = integers.stream()
                    .mapToInt(Integer::intValue)
                    .distinct()
                    .toArray();
        }
        record.setSendUserId(webContext.getCurrentUser().getUserId());
        record.setDeleted(0);
        record.setCreateTime(dateToString(new Date()));
        informMapper.insertSelective(record);
        Map<String,Object> map=new HashMap<>();
        System.out.println(record.getInformId());
        InformVM informVM=new InformVM();
        informVM.setInformId(record.getInformId());
        informVM.setCreateTime(dateToString(new Date()));

        map.put("inform",informVM);
        map.put("acceptUserId",uids);
        informMapper.insertBatch(map);

        return 1;
    }

    @Override
    public PageInfo<UserGroupVm> selectUsersGroup(UserGroupVm userGroupVm) {
        PageHelper.startPage(userGroupVm.getPageIndex(),userGroupVm.getPageSize());
        List<UserGroupVm> list = userMapper.selectUserGroup(userGroupVm);
        PageInfo<UserGroupVm> pageInfo = new PageInfo<UserGroupVm>(list);
        return pageInfo;
    }

    @Override
    public List<AcceptInformVM> selectAcceptInform(AcceptInformVM model) {
        return informMapper.selectAcceptInform(model);
    }

    @Override
    public List<InformDto> selectListForStudent(Integer userId, Integer isRead) {
        return informMapper.selectListForStudent(userId, isRead);
    }
    @Override
    public List<InformDto> selectListForTeacher(Integer sendUserId) {
        return informMapper.selectListForTeacher(sendUserId);
    }
    @Override
    public InformDto selectDetailForStudent(Integer informId) {
        return informMapper.selectDetailForStudent(informId);
    }
    @Override
    public List<InformDto> selectDetailForTeacher(Integer informId) {
        return informMapper.selectDetailForTeacher(informId);
    }

    @Override
    public void markAllAsReadByUserId(Integer userId) {
        informMapper.markAllAsReadByUserId(userId);
    }
    @Override
    public void markAsRead(Integer childId) {
        informMapper.markAsReadByChildId(childId);
    }
}
